1838. 最高频元素的频数
为保证权益,题目请参考 1838. 最高频元素的频数(From LeetCode).
解决方案1
Python
python
# 1838. 最高频元素的频数
# https://leetcode-cn.com/problems/frequency-of-the-most-frequent-element/
from typing import List
class Solution:
def maxFrequency(self, nums: List[int], k: int) -> int:
nums = sorted(nums)
left = 0
sums = 0
ans = 1
right = 1
while right < len(nums):
sums += (right - left) * (nums[right] - nums[right - 1])
while sums > k and left <= right:
sums -= nums[right] - nums[left]
left += 1
ans = max(ans, right - left + 1)
right += 1
return ans
if __name__ == "__main__":
so = Solution()
print(so.maxFrequency([1, 4, 8, 13], 5))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26